//
//  YMBingDeviceCodeVC.m
//  YMMedical
//
//  Created by y s on 2023/7/17.
//

#import "YMBingDeviceCodeVC.h"
#import "IQKeyboardManager.h"
#import "YMHomeService.h"
#import "UINavigationController+FDFullscreenPopGesture.h"


@interface YMBingDeviceCodeVC ()

@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UILabel *lblName;

@property (nonatomic, strong) UITextField *tfInput;
@property (nonatomic, strong) UIButton *btnScan;
@property (nonatomic, strong) UIButton *btnBind;


@end

@implementation YMBingDeviceCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

#pragma mark -- UI
- (void)setUI{
    self.fd_interactivePopDisabled = YES;
    [self showNavTitle:KLanguage(@"bind_device_txt")];
    [self.view addSubview:self.vBg];
    [self.vBg addSubview:self.lblName];
    [self.vBg addSubview:self.tfInput];
    [self.vBg addSubview:self.btnScan];
    [self.view addSubview:self.btnBind];
    
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:15]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:54]);
    }];
    [self.lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo([SFitUtil fit:15]);
        CGFloat width = [SFitUtil sizeOf:self.lblName].width+[SFitUtil fit:15];
        make.width.mas_equalTo(width);
    }];
    [self.tfInput mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblName.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.right.equalTo(self.btnScan.mas_left);
    }];
    [self.btnScan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo([SFitUtil fit:-5]);
        make.width.mas_equalTo([SFitUtil fit:24+20]);
    }];
    [self.btnBind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vBg.mas_bottom).offset([SFitUtil fit:20]);
        make.left.right.equalTo(self.vBg);
        make.height.mas_equalTo([SFitUtil fit:54]);
    }];
    
}


- (void)textFiledValueChange:(UITextField *)sender{
    NSString *text = [sender.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (text.length > 20){
        text = [text substringToIndex:20];
    }
    sender.text = text;
}
- (void)scanClickAction:(id)sender{
    WEAK_SELF;
    [YMBridging gotoScanCodeVC:NO isCard:NO finishBlock:^(NSString * _Nonnull valueStr) {
        weakSelf.tfInput.text = valueStr;
    }];
}

- (void)bindClickAction:(id)sender{
    NSString *snStr = self.tfInput.text;
    if (snStr == nil || snStr.length < 1){
        [SHUD showInfo:KLanguage(@"input_scan_sn_txt")];
        return;
    }
    [self checkDeviceIsTrue:snStr];
}
//检查是否是有此设备
- (void)checkDeviceIsTrue:(NSString *)snCode{
    WEAK_SELF;
    [SHUD showWait:self.view];
    [YMHomeService requestHomeDeviceIsTrueDataWithSn:snCode success:^(BOOL isTrue, NSDictionary * _Nonnull dictInfor) {
        if (isTrue){
            [weakSelf checkHadDevice:snCode dictInfot:dictInfor];
        }else{
            [SHUD hide:weakSelf.view];
            [SHUD showInfo:@"不存在该设备"];
        }
    } failure:^(NSString * _Nonnull message) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:@"设备添加失败，请稍后重试"];
    }];
}
//检查是否已经添加了该设备
- (void)checkHadDevice:(NSString *)snCode
             dictInfot:(NSDictionary *)dictInfor{
    WEAK_SELF;
    [YMHomeService requestHomeCheckHadDeviceDataWithSN:snCode success:^(BOOL hadDevice) {
        if (hadDevice){
            [SHUD hide:weakSelf.view];
            [SHUD showInfo:KLanguage(@"device_added_txt")];
        }else{
            [weakSelf requestAddDeviceData:dictInfor];
        }
    } failure:^(NSString * _Nonnull message) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:@"设备添加失败，请稍后重试"];
    }];
}
//添加设备
- (void)requestAddDeviceData:(NSDictionary *)dictInfor{
    WEAK_SELF;
    [YMHomeService requestHomeAddNewDeviceDataWithInfor:dictInfor success:^(NSDictionary * _Nonnull dictInfor) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:@"设备添加成功"];
        [SJumpUtil popVC:weakSelf animated:YES];
    } failure:^(NSString * _Nonnull message) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:@"设备添加失败，请稍后重试"];
    }];
}

#pragma mark -- setter
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.clipsToBounds = YES;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vBg;
}
- (UILabel *)lblName{
    if (_lblName == nil){
        _lblName = [SThemeUtil boldLabel:KLanguage(@"device_sn") color:STheme.H_D1 fontSize:16];
        _lblName.adjustsFontSizeToFitWidth = YES;
        _lblName.minimumScaleFactor = 0.6;
    }
    return _lblName;
}
- (UITextField *)tfInput{
    if (_tfInput == nil){
        _tfInput = [[UITextField alloc] init];
        _tfInput.backgroundColor = [UIColor clearColor];
        _tfInput.textColor = STheme.D1;
        _tfInput.font = [SFitUtil fitBoldFont:16];
        _tfInput.clearButtonMode = NO;
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:KLanguage(@"input_or_scan") attributes:@{NSForegroundColorAttributeName: STheme.D3, NSFontAttributeName: [SFitUtil fitFont:16]}];
        _tfInput.attributedPlaceholder = attr;
        _tfInput.keyboardType = UIKeyboardTypeASCIICapable;
        [_tfInput addTarget:self action:@selector(textFiledValueChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _tfInput;
}
- (UIButton *)btnScan{
    if (_btnScan == nil) {
        _btnScan = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnScan setImage:[UIImage imageNamed:@"home_bind_device_scan_icon"] forState:UIControlStateNormal];
        [_btnScan addTarget:self action:@selector(scanClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnScan;
}
- (UIButton *)btnBind{
    if (_btnBind == nil) {
        _btnBind = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnBind.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnBind setTitleColor:STheme.D9 forState:UIControlStateNormal];
        [_btnBind setTitle:KLanguage(@"sure_bind") forState:UIControlStateNormal];
        [_btnBind addTarget:self action:@selector(bindClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnBind.clipsToBounds = YES;
        _btnBind.layer.cornerRadius = [SFitUtil fit:10];
        _btnBind.backgroundColor = STheme.D12;
    }
    return _btnBind;
}


@end
